import axios from 'axios'
import qs from 'qs'

export default {
	namespaced: true,
	actions: {
		getCategory: function ({commit}, params) {
			let {callback} = params;
			params.onget = ({data}) => {
				callback(data);
			}
			commit("getCategory", params);
		},
		getAllCategory: function ({commit}, params) {
			let {callback} = params;
			params.onget = ({data}) => {
				callback(data);
			}
			commit("getAllCategory", params);
		},
		addCategory: function ({commit}, params) {
			let {callback} = params;
			params.onget = ({data}) => {
				callback(data);
			}
			commit("addCategory", params);
		},
		updateCategory: function ({commit}, params) {
			let {callback} = params;
			params.onget = ({data}) => {
				callback(data);
			}
			commit("updateCategory", params);
		},
		deleteCategory: function ({commit}, params) {
			let {callback} = params;
			params.onget = ({data}) => {
				callback(data);
			}
			commit("deleteCategory", params);
		},
	},
	mutations: {
		getCategory: function (state, {id, onget}) {
			axios.get(`/admin/category/${id}`).then(data => {
				onget(data);
			})
		},
		getAllCategory: function (state, {name, userIds, onget}) {
			axios.get(`/admin/category`, {
				params: {
					name,
					userIds
				}
			}).then(data => {
				onget(data);
			})
		},
		addCategory: function (state, {name, userIds, onget}) {
			axios.post(`/admin/category`, qs.stringify({
				name,
				userIds
			})).then(data => {
				onget(data);
			})
		},
		updateCategory: function (state, {id, name, userIds, onget}) {
			axios.put(`/admin/category`, qs.stringify({
				id,
				name,
				userIds
			})).then(data => {
				onget(data);
			})
		},
		deleteCategory: function (state, {id, onget}) {
			axios.delete(`/admin/category`,qs.stringify({
				id
			})).then(data => {
				onget(data);
			})
		},
	}
	
}